package com.ksd.modules.note.controller;

import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.note.po.AdminNoteChapterSavePO;
import com.ksd.modules.note.po.AdminNoteVideoPO;
import com.ksd.modules.note.service.IAdminNoteChapterService;
import com.ksd.modules.note.vo.AdminNoteChapterVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AdminNoteChapterController extends BaseController {
    
    @Autowired
    private IAdminNoteChapterService adminNoteChapterService;

    /**
     * 【小册】-【章列表】
     * @return
     */
    @PostMapping("/note/chapter/list")
    public R<List<AdminNoteChapterVo>> findAdminNoteChapters(@RequestParam("noteId")Long noteId){
        return R.data(adminNoteChapterService.findAdminNoteChapters(noteId));
    }

    /**
     * 【小册】-【章】-保存和更新
     * @param adminNoteChapterSavePO
     * @return
     */
    @PostMapping("/note/chapter/saveupdate")
    @ApiLog("【小册】-【章】-保存和更新")
    @RepeatSubmit
    public R<Boolean> saveUpdateAdminNoteChapter(@Validated @RequestBody AdminNoteChapterSavePO adminNoteChapterSavePO){
        return R.data(adminNoteChapterService.saveUpdateAdminNoteChapter(adminNoteChapterSavePO));
    }

    /**
     * 【小册】-【章】-根据id明细查询
     * @param id
     * @return
     */
    @PostMapping("/note/chapter/get/{id}")
    public R<AdminNoteChapterVo> getAdminNoteChapterById(@PathVariable("id")Long id){
        return R.data(adminNoteChapterService.getAdminNoteChapterById(id));
    }

    /**
     * 【小册】-【章】- 根据ID删除
     * @param id
     * @return
     */
    @PostMapping("/note/chapter/remove/{id}")
    @ApiLog("【小册】-【章】- 根据ID删除")
    public R<Boolean> removeAdminNoteChapterById(@PathVariable("id")Long id){
        return R.data(adminNoteChapterService.removeAdminNoteChapterById(id));
    }

    /**
     *【小册】-【章】- 修改状态
     * @return
     */
    @PostMapping("/note/chapter/status/{id}/{field}")
    @ApiLog("【小册】-【章】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminNoteChapterService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminNoteChapterService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     *【小册】-【章】- 修改状态
     * @return
     */
    @PostMapping("/note/chapter/updatevideo")
    @ApiLog("【小册】-【章】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateVideo(@RequestBody AdminNoteVideoPO noteVideoPO) {
        return R.data(adminNoteChapterService.updateStatusStr(noteVideoPO.getId(), "videoid", noteVideoPO.getValue()));
    }

    /**
     * 【小册】-【章】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/note/chapter/copy/{opid}")
    @ApiLog("【小册】-【章】-复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminNoteChapterService.copyData(opid));
    }
}
